/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.listener.storage;

import org.apache.log4j.*;
import org.hibernate.*;
import org.hibernate.criterion.*;
import java.util.*;

public class SubscriptionDAO
{
      Session session = null;
      private static final Logger logger = Logger.getLogger (SubscriptionDAO.class);

    /**
     * Default constructor
     */
    public SubscriptionDAO ()
    {
        this.session = DataBaseManager.getSessionFactory ().getCurrentSession ();
    }


    public boolean saveSubscriber (Subscription subscription)
    {
        try
        {
            Transaction tx = session.beginTransaction ();
            session.save (subscription);
            tx.commit ();
            logger.info ("Subscription saved successfully");
            return true;
        }
        catch (HibernateException ex)
        {
            logger.error ("Error in saving subscriber", ex);
            return false;
        }
    }
   public Subscription getSubscriberDetails(String subscriptionID,String topic)
   {
       Subscription subscription=null;
       ArrayList<Subscription> retrievedSubscriptions;

       try
       {
          Transaction tx=session.beginTransaction();
          Criteria criteria=session.createCriteria(Subscription.class).add(Restrictions.eq("subscriptionID",subscriptionID)).add(Restrictions.eq("filter",topic));
          List<Subscription> subList=criteria.list();
          subscription=subList.get(0);
          System.out.println(subscription);

       }
       catch(Exception ex)
       {
          ex.printStackTrace();
       }

       return subscription;
   }
   public void deleteSubscription(Subscription subscription)
   {
        try
        {
            Transaction tx = session.beginTransaction ();
            session.delete (subscription);
            tx.commit ();
            logger.info ("Subscription deleted successfully");
           // return true;
        }
        catch (HibernateException ex)
        {
            logger.error ("Error in deleting subscriber", ex);
           // return false;
        }
   }
}
